package com.briup.gsauestore.service.impl;

import com.briup.gsauestore.bean.RecommendShop;
import com.briup.gsauestore.bean.Shop;
import com.briup.gsauestore.dao.IRecommendShopDao;
import com.briup.gsauestore.dao.IShopDao;
import com.briup.gsauestore.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author adam
 * @date 2022/1/12
 */
@Service
public class ShopServiceImpl implements IShopService {
    @Autowired
    private IShopDao shopDao;
    @Autowired
    private IRecommendShopDao recommendShopDao;

    @Override
//    查询所有非特价商品
    public List<Shop> findAllShops() {
        List<Shop> list = shopDao.findByDiscount(false);
        return list;
    }

    @Override
    public Shop findShopById(Long id) {
        Optional<Shop> optional = shopDao.findById(id);
        Shop shop = optional.get();
        return shop;
    }

    @Override
//    查询所有的特价商品
    public List<Shop> findDiscount() {
        List<Shop> list = shopDao.findByDiscount(true);
        return list;
    }

    @Override
    public List<Shop> recommendShop(Long userId) {
        List<RecommendShop> list = recommendShopDao.findByUserId(userId);
//        新用户没推荐数据
        if (list.size() < 1) {
            List<Shop> shops = shopDao.findTop10BySalesVolume();
            return shops;
        }else {
//            List<Shop> shops = list.stream().map(i -> i.getShops()).collect(Collectors.toList());
            ArrayList<Shop> shops = new ArrayList();
            for (RecommendShop recommendShop : list) {
                Shop shop = recommendShop.getShops();
                shops.add(shop);
            }
            return shops;
        }


    }

    @Override
    public List<Shop> findByCategory(Long categoryId) {
        List<Shop> list = shopDao.findByCategoryId(categoryId);
        return list;
    }

    @Override
    public List<Shop> searchShop(String shopName) {

        List<Shop> list = shopDao.findByNameContaining(shopName);
        return list;
    }
}
